const express = require("express");

const router = express.Router();

const cartModel = require("../../model/cartModel");

// 导入jwt token工具
const JwtUtil = require("../../utils/jwt");
// 导入生成id的包
const { v4: uuidv4 } = require("uuid");

router.post("/addcart", async (req, res) => {
    // 判断用户登录状态
    let token = req.headers["token"] || req.query.token;
    console.log("1.0token", token);
    // 请求需要进行token校验
    let jwt = new JwtUtil(token);
    console.log("jwt===>", jwt);
    // 返回一个对象（包含用户身份）
    let results = jwt.verifyToken();
    console.log("res===>", results);
    // console.log(result);
    if (results == "err") {
        res.send({
            code: -1,
            msg: "登录已过期,请重新登录",
        });
    } else {
        let { gid, goods_img, goods_name, goods_price, goods_color, goods_size, goods_num } = req.body;
        let cart_id = uuidv4();
        await cartModel
            .add_cart(cart_id, gid, goods_img, goods_name, goods_price, goods_color, goods_size, goods_num)
            .then((content) => {
                res.send({
                    code: 200,
                    msg: "购物车添加成功",
                    content,
                });
            })
            .catch((error) => {
                res.send({
                    code: -1,
                    msg: "购物车添加失败",
                    error,
                });
            });
    }
});

router.post("/updatecart", async (req, res) => {
    let token = req.headers["token"] || req.query.token;
    console.log("2.0token", token);
    // 请求需要进行token校验
    let jwt = new JwtUtil(token);
    // 返回一个对象（包含用户身份）
    let result = jwt.verifyToken();
    if (result == "err") {
        res.send({
            code: -1,
            msg: "登录已过期,请重新登录",
        });
    } else {
        let { cart_id, goods_num, goods_color, goods_size } = req.body;
        await cartModel
            .update_cart(cart_id, goods_color, goods_size, goods_num)
            .then((content) => {
                res.send({
                    code: 200,
                    msg: "修改成功",
                });
            })
            .catch((err) => {
                res.send({
                    code: -1,
                    msg: "修改失败",
                    err,
                });
            });
    }
});

router.get("/getcart", async (req, res) => {
    let token = req.headers["token"] || req.query.token;
    // console.log("2.0token", token);
    // 请求需要进行token校验
    let jwt = new JwtUtil(token);
    // 返回一个对象（包含用户身份）
    let result = jwt.verifyToken();

    if (result == "err") {
        res.send({
            code: -1,
            msg: "登录已过期,请重新登录",
        });
    } else {
        await cartModel
            .get_cart()
            .then((content) => {
                res.send({
                    code: "200",
                    msg: "获取购物车数据成功",
                    content,
                });
            })
            .catch((error) => {
                res.send({
                    code: -1,
                    msg: "获取购物车数据失败",
                    error,
                });
            });
    }
});

router.post("/removecart", async (req, res) => {
    let { gids } = req.body;
    await cartModel
        .remove_cart(gids)
        .then((content) => {
            res.send({
                code: 200,
                msg: "删除购物车成功",
            });
        })
        .catch((err) => {
            res.send({
                code: -1,
                msg: "删除失败",
                err,
            });
        });
});

module.exports = router;
